/**
 * @author helio
 */
var NewsManageView = Ext.extend(BasicPanel,{
	inputWin : null,
	
	initlize : function() {
			
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'news',
					remoteSort: true,
			        fields: ['id', 'title', 'author', 'category', 'date']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm,{
					header: "标题",
					dataIndex: 'title',
					sortable: true
				},{
					header: "作者",
					dataIndex: 'author',
					width: 60,
					sortable: true
				},{
					header: "分类",
					dataIndex: 'category',
					width: 60,
					sortable: true
				},{
					header: "发布日期",
					dataIndex: 'date',
					width: 60,
					renderer: Ext.util.Format.dateRenderer('Y-m-d'),
					sortable: true
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'新闻发布',
		            tooltip:'新闻发布',
		            iconCls:'add',
					handler: this.addNews.createDelegate(this)
		        }, '-', {
		            text:'新闻编辑',
		            tooltip:'新闻编辑',
		            iconCls:'option',
					handler: this.modifyNews.createDelegate(this)
		        },'-',{
		            text:'删除',
		            tooltip:'删除选中的新闻',
		            iconCls:'remove',
					handler: this.deleteNews.createDelegate(this)
		        }],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'title',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this),
					
					rowdblclick : this.modifyNews.createDelegate(this)
				}
			});
			
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['news'] = resultJson.news;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.getNewsListJson(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	showEditeWin : function() {
		if(!this.inputWin) {
			this.inputForm = new Ext.FormPanel({
 				frame: true,
				labelWidth: 20,
				defaultType: 'textfield',
				layout: 'border',
				items: [{
	 				xtype: 'fieldset',
	 				layout: 'table',
	 				region: 'north',
	 				border: true,
	 				title: '标题',
	 				autoHeight: true,
	 				defaults: {layout: 'form', border: false},
	 				
	 				items: [{
	 					xtype: 'textfield',
	 					name: 'title',
	 					width: 350,
	 					anchor: '95%'
	 				},{
	 					width: 300,
	 					bodyStyle: 'background-color: transparent;',
	 					items: [{
	 						xtype: 'radiogroup',
	 						//id: 'category',
				            items: [
				                {boxLabel: '企业动态', name: 'category', inputValue: '企业动态'},
				                {boxLabel: '业内新闻', name: 'category', inputValue: '业内新闻'},
				                {boxLabel: '内部公告', name: 'category', inputValue: '内部公告', checked: true}
				            ]
	 					}]		 					
					}]
					
	 			},{
	 				xtype: 'panel',
	 				layout: "fit",
	 				border: true,
	 				region: 'center',
	 				
	 				items: {
	 					xtype: 'htmleditor',
	 					name: 'content',
	 					anchor: '98%'
	 				}
	 			}],
				
				buttons: [{
		            text: '保存',
					listeners: {
						"click": function(){
							this.synchronizeJson();
							this.params = this.inputForm.form.getValues();
							if(this.params['category']) {
								this.formDataJson['category'] = this.params['category'];
							}
							if(!this.formDataJson['author']) {
								this.formDataJson['author'] = SALE.App.userInfo.name;
							}
							                  
							if(this.formDataJson.title && this.formDataJson.title != "") {
								this.saveNewsInfo();
							}
							
						}.createDelegate(this)
					}
		        },{
		            text: '取消',
					listeners:{
						"click":function(){
							this.inputForm.form.reset();
						}.createDelegate(this)
					}
		        }]
			});
			
	 		this.inputWin = new Ext.Window({
	 		
	 			title: '新闻发布/编辑',
				closable: true,	
				collapsible: true,
				width: 730,
				height: 550,
				layout: 'fit',
				closeAction :'hide',
				
				items: this.inputForm,
				
				listeners: {
					'show': function() {
						this.synchronizeForm();	
						if(this.formDataJson && this.formDataJson['category']) {
							this.setRadioGroupValue('category', this.formDataJson['category']);
						}
					}.createDelegate(this),
					
					'hide': function() {							
						this.clear();
					}.createDelegate(this)
				}
	 		});							
		}
		
		this.inputWin.show();
	},
	
	addNews : function() {
		this.task = "new";
		if(this.formDataJson) {
			this.formDataJson.id="";
			this.clear();
		}
		this.showEditeWin();
	},
	
	modifyNews : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			SALE.Utils.showMessageBox('请先选择一条数据！');
		} else if(recordList.length > 1) {
			SALE.Utils.showMessageBox('每次只能编辑一条数据，请确认只选中了一条数据！');
		} else {
			var id= recordList[0].get("id");
			
			SALE.Action.getNewsDetailJson(this.showModifyDataHandler.createDelegate(this), id);				
		}
	},
	
	deleteNews : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			SALE.Utils.showMessageBox('请选择你要删除的数据！');
		} else {
			Ext.MessageBox.confirm('删除确认', '你真的要删除选中的新闻信息吗?', function(btn) {
				if (btn == "yes") {
					SALE.Action.deleteNewsInfo(this.deleteSuccessHandler.createDelegate(this), this.selectedRecordList);
				}
			}.createDelegate(this));
		}
	},
	
	saveNewsInfo : function() {
		SALE.Action.saveNewInfo(this.saveSuccessHandler.createDelegate(this), this.formDataJson, this.task);
	}	
});

NewsManageView.showListPanel = function() {	
	var inputView = new NewsManageView();
	SALE.App.showOperateTabPanel(inputView.getCorePanel(), "新闻发布");
}
